<script setup lang="ts">

import { downloadContractAPI, getParkDetail } from '@/api/park';
import type { EnterpriseDetail, Rent } from '@/types/park';
import { onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';

const route = useRoute()
// 租赁合同列表
const getDetailList = ref<EnterpriseDetail>({} as EnterpriseDetail)
const getDetail = async () => {
    if(route.params.id){
        const res = await getParkDetail(route.params.id as string)
        console.log('res',res.data);
        getDetailList.value = res.data
    }
}
onMounted(() => {
    getDetail()
})
const getPreviewUrl=(url:string)=>{
// 获取到文件后缀   split将文件按.分割,pop()取数组最后一个元素
    const houzhui = url.split('.').pop()
    // console.log('row',houzhui);
    if(houzhui === 'pdf'){
        return url
    }else if(houzhui === 'doc' || houzhui === 'docx'){
        return `https://view.officeapps.live.com/op/view.aspx?src=${url}`
    }
}
/**
 * 异步下载租赁合同
 *
 * @param {Rent} row - 租赁信息对象，包含合同ID和名称等信息
 */
 const Downloadrent = async (row: Rent) => {
  // 调用下载合同API，传入合同ID
  const res = await downloadContractAPI(row.contractId)
  // 创建一个新的<a>元素用于下载
  const aElement = document.createElement('a')
  // 将API返回的文件流转换为URL，并设置为<a>元素的href属性
  aElement.href = URL.createObjectURL(res)
  // 设置下载文件的名称
  aElement.download = row.contractName
  // 触发<a>元素的点击事件，开始下载
  aElement.click()
  // 下载完成后，释放创建的URL对象，以节省资源
  URL.revokeObjectURL(aElement.href)
  // 从DOM中移除<a>元素
  aElement.remove()
}
</script>

<template>
  <div class="detail-enterprise">
    <header class="add-header">
      <el-page-header content="查看企业" @back="$router.back()" />
    </header>
    <main class="add-main">
        <div class="form-container">
        <div class="title">企业信息</div>
        <div class="table-container">
            <el-form :model="getDetailList" :inline="true" >
            <el-row style="margin-left:200px ;">
              <el-col :span="12">
                <el-form-item label="企业名称">{{ getDetailList.name }}</el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="法人">{{ getDetailList.legalPerson }}</el-form-item>
              </el-col>
            </el-row>
            <el-row style="margin-left:200px ;">
              <el-col :span="12">
                <el-form-item label="注册地址">{{ getDetailList.registeredAddress }}</el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="行业">{{ getDetailList.industryName }}</el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="营业执照" style="margin-left:200px ;">
              <img :src="getDetailList.businessLicenseUrl" alt="" width="200px">
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">联系人信息</div>
        <div class="table-container">
          <el-form :model="getDetailList" :inline="true" >
            <el-row style="margin-left:200px ;">
              <el-col :span="12">
                <el-form-item label="企业联系人">{{ getDetailList.contact }}</el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系电话">{{ getDetailList.contactNumber }}</el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">租赁记录</div>
        <div class="table-container">
          <el-table :data="getDetailList.rent" style="width: 100%" border>
            <el-table-column type="index" label="序号" width="50" />
            <el-table-column prop="name" label="租赁楼宇" width="180" />
            <el-table-column label="租赁起止时间" width="280">
              <template #default="{ row }">
                {{ row.startTime }} - {{ row.endTime }}
              </template>
            </el-table-column>
            <el-table-column label="租赁合同(点击预览)">
              <template #default="{ row }">
                <el-button type="primary" link>
                  <a class="link" :href="getPreviewUrl(row.contractUrl)" target="_blank">
                    {{ row.contractName }}
                  </a>
                </el-button>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="录入时间" />
            <el-table-column prop="address" label="操作">
              <template #default="{ row }">
                <el-button type="primary" link @click="Downloadrent(row)">合同下载</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </main>

  </div>
</template>


<style scoped lang="scss">
.link{
  text-decoration: none;
  color:inherit
}
.detail-enterprise {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;
      margin-bottom: 20px;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }
    }

    .table-container {
      padding: 20px;
    }

    .preview {
      img {
        width: 100px;
      }
    }
  }
}
</style>